package com.aek.aekdroid.data;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.aek.aekdroid.MyLog;

public class DbHelper extends SQLiteOpenHelper implements IDbConstants {

	static final String TAG = "DbHelper";

	Context context;

	public DbHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
		this.context = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		MyLog.i(TAG, "Creating database: " + db);
		MyLog.i(TAG, "Creating table: " + MATCHES_TABLE);
		String sql = "CREATE TABLE " + MATCHES_TABLE + " (" + MATCHES_C_ID + " INTEGER NOT NULL PRIMARY KEY UNIQUE, " + MATCHES_C_DATETIME
				+ " int, " + MATCHES_C_TOURNAMENT + " text, " + MATCHES_C_HOME_TEAM + " text, " + MATCHES_C_AWAY_TEAM + " text, "
				+ MATCHES_C_HOME_GOALS + " text, " + MATCHES_C_AWAY_GOALS + " text, " + MATCHES_C_PLAYERS + " text, " + MATCHES_C_STADIUM
				+ " text, " + MATCHES_C_CHANNEL + " text, " + MATCHES_C_PERIOD + " text, " + MATCHES_C_UPDATED + " int, "
				+ MATCHES_C_CALENDAR_ID + " text," + MATCHES_C_DELETED + " int DEFAULT 0 )";
		db.execSQL(sql);
		MyLog.i(TAG, "Creating table: " + DETAILS_TABLE);
		sql = "CREATE TABLE " + DETAILS_TABLE + " (" + DETAILS_C_ID + " INTEGER NOT NULL PRIMARY KEY UNIQUE, " + DETAILS_C_MATCH_ID
				+ " int, " + DETAILS_C_TYPE + " int, " + DETAILS_C_MIN + " double, " + DETAILS_C_PLAYER + " text, " + DETAILS_C_COMMENT
				+ " text, " + DETAILS_C_SIDE + " int, " + DETAILS_C_DELETED + " int DEFAULT 0 )";
		db.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + MATCHES_TABLE);
		db.execSQL("DROP TABLE IF EXISTS " + DETAILS_TABLE);
		MyLog.d(TAG, "onUpgrade");
		onCreate(db);
	}

}
